package com.itheima.prince.smartpeking.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

import com.itheima.prince.smartpeking.R;
import com.itheima.prince.smartpeking.utils.SPUtil;

public class WelcomeUI extends Activity {

    RelativeLayout rl_welcome_bg;
    public static final String IS_APP_FIRST_OPEN = "isfirstopen";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_ui);
        rl_welcome_bg = (RelativeLayout) findViewById(R.id.rl_welcome_bg);
        animation();
    }

    private void animation() {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, .5f,
        Animation.RELATIVE_TO_SELF, .5f);
        rotateAnimation.setDuration(2000);
        rotateAnimation.setFillAfter(true);

        ScaleAnimation scaleAnimation = new ScaleAnimation(
         0,  1,
         0, 1,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(2000);
        scaleAnimation.setFillAfter(true);

        AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
        alphaAnimation.setDuration(2000);
        alphaAnimation.setFillAfter(true);

        AnimationSet animationSet = new AnimationSet(false);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(alphaAnimation);

        rl_welcome_bg.startAnimation(animationSet);

        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //第一次安装时，进入引导界面
                //以后安装时候，再进入正常页面 用sp
                boolean isAppFirstOpen = SPUtil.getBoolean(getApplicationContext(), IS_APP_FIRST_OPEN, true);
                if (isAppFirstOpen){
                    Log.d("我在动画界面","跳入引导界面");
                    Intent intent = new Intent(WelcomeUI.this,GuideUi.class);
                    startActivity(intent);
                }else {
                    Log.d("我在动画界面","跳入主页面");
                    startActivity(new Intent(WelcomeUI.this,MainActivity.class));
                }
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
